package ks.app.smartDic;

import java.util.ArrayList;

import net.daum.mobilead.MobileAdView;


import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class ResultView extends Activity implements OnClickListener {

	private ListView mList;
	ImageView resultSearchBtn;
	ImageView resultVoiceBtn;
	EditText editTextSearchNameForResult;

	ArrayList<String> matches;
	String sortOfDictionary;

	//AdView adView;
	MobileAdView adViewAdam;
	MultipleAd mAd;
	

	GoogleAnalyticsTracker tracker;

	private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);

		Intent lastIntent = getIntent();
		sortOfDictionary = lastIntent
				.getStringExtra(getString(R.string.SORT_OF_DICTIONARY));

		resultSearchBtn = (ImageView) findViewById(R.id.resultSearchBtn);
		resultSearchBtn.setOnClickListener(this);

		resultVoiceBtn = (ImageView) findViewById(R.id.resultVoiceBtn);
		resultVoiceBtn.setOnClickListener(this);

		Intent intent = getIntent();

		matches = intent.getStringArrayListExtra("data");

		mList = (ListView) findViewById(R.id.mList);
		

		mList.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_single_choice, matches));

		mList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				String queryToSend = mList.getItemAtPosition(position)
						.toString();

				Intent tabIntent = new Intent(getApplicationContext(),
						tabView.class);
				tabIntent.putExtra("query", queryToSend);
				tabIntent.putExtra(getString(R.string.SORT_OF_DICTIONARY),
						sortOfDictionary);
				startActivity(tabIntent);
			}
		});

		editTextSearchNameForResult = (EditText) findViewById(R.id.editTextSearchNameForResult);
		editTextSearchNameForResult.setOnKeyListener(new OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN
						&& keyCode == KeyEvent.KEYCODE_ENTER) {

					goTab();

				}
				return false;
			}
		});
 
		adViewAdam = (MobileAdView) findViewById(R.id.adviewAdam);

		mAd = new MultipleAd(getApplicationContext(), adViewAdam);
		mAd.displayAd();
		
		/*
		adView = new AdView(this, AdSize.BANNER,
				getString(R.string.ADMOB_PUBLISHER_ID));
		RelativeLayout layout = (RelativeLayout) findViewById(R.id.linear);

		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
				RelativeLayout.TRUE);
		layout.addView(adView, layoutParams);
		adView.loadAd(new AdRequest());
		 */

		tracker = GoogleAnalyticsTracker.getInstance();
		tracker.start("UA-22873894-1", 20, this);
		tracker.trackPageView("/resultview");
		tracker.dispatch();

	}

	private void goTab() {
		if (editTextSearchNameForResult.getText().length() == 0) {
			Toast.makeText(getApplicationContext(), "검색어를 입력해 주세요.",
					Toast.LENGTH_SHORT).show();
			return;
		}

		SharedPreferences sp;
		String[] listName = getResources().getStringArray(
				R.array.entries_list_preference);
		sp = getSharedPreferences("PrefSmartDic", 0);
		String targetClassName = sp.getString(
				getString(R.string.PREF_RESULT_VIEW), listName[1]);

		Intent goSearch;

		if (targetClassName.compareTo(listName[0]) == 0)
			goSearch = new Intent(getApplicationContext(), TabOriginal.class);
		else
			goSearch = new Intent(getApplicationContext(), tabView.class);

		goSearch.putExtra("query", editTextSearchNameForResult.getText()
				.toString());
		goSearch.putExtra(getString(R.string.SORT_OF_DICTIONARY),
				sortOfDictionary);
		editTextSearchNameForResult.setText(null);

		startActivity(goSearch);
	}

	public void onClick(View v) {

		if (v == resultSearchBtn) {

			goTab();

		} else if (v == resultVoiceBtn) {

			startVoiceRecognitionActivity();

		}

	}

	private void startVoiceRecognitionActivity() {

		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

		if (sortOfDictionary.compareTo(getString(R.string.KOREAN_TO_ENG)) == 0
				|| sortOfDictionary
						.compareTo(getString(R.string.KOREAN_TO_KOREAN)) == 0) {

			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "ko");
			intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "단어를 말씀해 주세요.");

		} else {

			intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
			intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
					"Please speak an English Word.");
		}

		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);

		startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
				&& resultCode == RESULT_OK) {

			mList.clearChoices();

			matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

			mList.setAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_single_choice, matches));

			mList.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					String queryToSend = mList.getItemAtPosition(position)
							.toString();

					Intent tabIntent = new Intent(getApplicationContext(),
							tabView.class);
					tabIntent.putExtra("query", queryToSend);
					tabIntent.putExtra(getString(R.string.SORT_OF_DICTIONARY),
							sortOfDictionary);

					startActivity(tabIntent);
				}
			});

		}

		super.onActivityResult(requestCode, resultCode, data);

	}

	protected void onResume() {

		super.onResume();

		mAd.displayAd();
	}

}
